<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Gold extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	function __construct(){
		parent::__construct();
		$this->load->model("admin/banner_model",'banner_model');
		$this->load->model("user/user_model",'user_model');
		$this->lang->load('main', 'thai');
	}
	
	public function index()
	{
		$header['title'] = config_item('site_name').' : '.$this->lang->line('gold');
                $data['block_title'] = $this->lang->line('gold');
		$this->template->set_template('page');
		$this->template->write_view('header','master/header',$header , false);
		
		//$banner = $this->banner_model->get_banner();
		
		$this->load->model('admin/member_model','member_model');
		$data['detail'] = htmlspecialchars_decode($this->member_model->get_member_message());
		
		$banner['user_block'] = get_user_box();
		$this->template->write_view('sideleft','home/master/sidebar_left',$banner);
		$this->template->write_view('sideright','home/master/sidebar_right',$banner);
		
		$this->template->write_view('search_form','master/page_search');
		
		$this->template->write_view('content', 'gold' , $data);
		
		$this->template->write_view('footer','master/footer');
		$this->template->render();
	}
	
	public function register()
	{
		if($this->session->userdata('logged_in') == TRUE){
			$user_info = $this->user_model->getUserInfo($this->session->userdata('username'));
			
			//Var_Dump($user_info);
			//exit;
			$time = time();
			if($this->user_model->regis_gold($user_info['id'],$time)){
				$this->load->library('cimailer','cimailer');
				$file = APPPATH.'../assets/mail/user_gold_regis.txt';
				$handle = fopen($file,'r');
				$body = '';
				if ($handle) {
					while (!feof($handle)) {
						$file_ = fgets($handle, 4096);
						$body .= $file_."<br>";
					}
					fclose($handle);
				}
				
				try{
					//$this->cimailer->IsMail();
					$this->cimailer->CharSet="utf-8";
					$this->cimailer->IsHTML(true);
					$this->cimailer->From = $this->config->item('main_email');
					
					$body = str_replace('{NAME}',$user_info['firstname'],$body);
					$body = str_replace('{LASTNAME}',$user_info['lastname'],$body);
					$body = str_replace('{SITENAME}',$this->config->item('site_name'),$body);
					$body = str_replace('{GOLDURL}','<a href="'.site_url().'gold" title="'.$this->lang->line('gold_register').'">'.$this->lang->line('gold').'</a>',$body);
					$body = str_replace('{EMAIL}',$this->config->item('main_email'),$body);
					$body = str_replace('{PAIDURL}','<a href="'.site_url().'paid-form" title="'.$this->lang->line('paid_form').'">'.$this->lang->line('paid_form').'</a>',$body);
					
					$this->cimailer->FromName = $this->config->item('site_name').' : '.$this->lang->line('gold_register');
					$this->cimailer->AddReplyTo($this->config->item('main_email'), $this->config->item('site_name').' : '.$this->lang->line('gold_register'));
					$this->cimailer->AddAddress($user_info['email']);
					$this->cimailer->Subject = $this->config->item('site_name').' : '.$this->lang->line('gold_register');
					$this->cimailer->Body = $body;
					
					if($this->cimailer->Send()){
						$data['message'] = $this->lang->line('gold_register_complete');
						$data['message'] = str_replace('{SITENAME}',$this->config->item('site_name'),$data['message']);
						$data['message'] = str_replace('{EMAIL}',$this->config->item('main_email'),$data['message']);
						$data['message'] = str_replace('{PAIDURL}','<a href="'.site_url().'paid-form" title="'.$this->lang->line('paid_form').'">'.$this->lang->line('paid_form').'</a>',$data['message']);
					}else{
						$data['message'] = $this->lang->line('mail_not_send');
						$data['message'] .= '<br />'.$this->cimailer->ErrorInfo;
					}
				} catch (phpmailerException $e) {
					echo $e->errorMessage(); //Pretty error messages from PHPMailer
					$data['message'] = $this->lang->line('mail_not_send');
				} catch (Exception $e) {
					echo $e->getMessage(); //Boring error messages from anything else!
					$data['message'] = $this->lang->line('mail_not_send');
				}
			}else{
				$data['message'] = $this->lang->line('data_not_save');
			}
			
			$banner['user_block'] = get_user_box();
			$data['block_title'] = $this->lang->line('gold_register');
			$header['title'] = $this->config->item('site_name').' : '.$this->lang->line('gold_register');
			
			$this->template->set_template('page');
			$this->template->write_view('header','home/master/header',$header , false);
			$this->template->write_view('sideleft','home/master/sidebar_left',$banner);
			$this->template->write_view('sideright','home/master/sidebar_right',$banner);
			$this->template->write_view('search_form','home/master/page_search');
			$this->template->write_view('content', 'result_message',$data);
			$this->template->write_view('footer','home/master/footer');
			$this->template->render();
		}else{
			redirect(site_url().'user/register');
		}
	}
}

/* End of file gold.php */
/* Location: ./application/modules/home/controllers/gold.php */